//
//  JWTSelectCourseSwipeView.m
//  JWT
//
//  Created by ndm on 17/2/23.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTSelectCourseSwipeView.h"
#import "QCListViewController.h"
#import "JWTStore.h"

#define RGB_GREEN RGB(29, 148, 78)

static NSString *selectStore_identifier = @"selectStore_identifier";

static NSString *selectCourse_identifier = @"selectCourse_identifier";

@interface JWTSelectCourseSwipeView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>



@property (nonatomic,strong) NSArray *courseArray;

@property (nonatomic,assign) NSInteger course_type;

@property (nonatomic,assign) NSInteger store_index;

@end

@implementation JWTSelectCourseSwipeView

SHARED_INSTANCE_IMPLEMENTATION(JWTSelectCourseSwipeView);
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame] ;
    if (self) {
        _storeArray = [[NSArray alloc] init];
        _course_type = [APPDELEGATE.courseType integerValue];
        _store_index = 0;
        _courseArray = @[@"成人课",@"儿童课",@"兴趣课"];
        _selectCourseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH*0.67, UISCREEN_HEIGHT)];
        _selectCourseView.backgroundColor = HeaderBackground_Color;
        [self addSubview:_selectCourseView];
        
        _iconBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, getNAVI_TOP_HEIGHT(), 60, 60)];
        _iconBtn.layer.borderWidth = 0.5;
        _iconBtn.layer.borderColor = RGB_LightGray.CGColor;
        [_iconBtn setImage:[UIImage imageNamed:@"my_user_default"] forState:UIControlStateNormal];
        [self addSubview:_iconBtn];
        
        //UserInfoHeader_dancing
        _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_selectCourseView.width-100, UISCREEN_HEIGHT*0.3-64, 80, 44)];
        [_backImageView setImage:[UIImage imageNamed:@"UserInfoHeader_dancing"]];
        [self addSubview:_backImageView];
        _iconBtn.center = CGPointMake(_selectCourseView.width*0.5, _iconBtn.center.y);
        
        _columNameView = [[UIView alloc] initWithFrame:CGRectMake(0, _backImageView.bottom+20, _selectCourseView.width, 44)];
        _columNameView.backgroundColor = RGB_GREEN;
        [_selectCourseView addSubview:_columNameView];
        
        UILabel *hintStoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (_columNameView.width-1)*0.5, 44)];
        hintStoreLabel.textColor = RGB_WhiteBackGround;
        hintStoreLabel.font = Text_Font15;
        hintStoreLabel.textAlignment = NSTextAlignmentCenter;
        hintStoreLabel.text = @"门店";
        [_columNameView addSubview:hintStoreLabel];
        
        _storeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,_columNameView.bottom, hintStoreLabel.width, UISCREEN_HEIGHT*0.5)];
        _storeTableView.dataSource = self;
        _storeTableView.delegate = self;
        _storeTableView.backgroundColor = RGB_WhiteBackGround;
        _storeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_selectCourseView addSubview:_storeTableView];
        [_storeTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:selectStore_identifier];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(hintStoreLabel.right, 14, 1, 44-28)];
        line.backgroundColor = RGB_WhiteBackGround;
        [_columNameView addSubview:line];
        
        UILabel *hintCourLabel = [[UILabel alloc] initWithFrame:CGRectMake(hintStoreLabel.right+1, 0, (_columNameView.width-1)*0.5, 44)];
        hintCourLabel.textColor = RGB_WhiteBackGround;
        hintCourLabel.font = Text_Font15;
        hintCourLabel.textAlignment = NSTextAlignmentCenter;
        hintCourLabel.text = @"课程";
        [_columNameView addSubview:hintCourLabel];
        
        
        
        _courTableView = [[UITableView alloc] initWithFrame:CGRectMake(hintStoreLabel.right,_columNameView.bottom, hintStoreLabel.width+1, UISCREEN_HEIGHT*0.5)];
        _courTableView.dataSource = self;
        _courTableView.delegate = self;
        _courTableView.backgroundColor = RGB_LightGrayBackGround;
        [_selectCourseView addSubview:_courTableView];
        _courTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_courTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:selectCourse_identifier];
        
        
        _swipeView = [[UIView alloc] initWithFrame:CGRectMake(_selectCourseView.width, _selectCourseView.top, UISCREEN_WIDTH-_selectCourseView.width, _selectCourseView.height)];
        _swipeView.backgroundColor = [UIColor clearColor];
        
        //
        UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToSelectCourse:)];
        swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
        [_swipeView addGestureRecognizer:swipeLeft];
        
        UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToSelectCourse:)];
        swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
        swipeRight.delegate = self;
        [_swipeView addGestureRecognizer:swipeRight];
        
        //刷新视图
        _refreshBtn = [[UIButton alloc] initWithFrame:CGRectMake(_storeTableView.width+Cell_Margin, _columNameView.bottom+UISCREEN_HEIGHT*0.5+3*Cell_Margin, 22, 22)];
        [_refreshBtn setBackgroundImage:[UIImage imageNamed:@"my_user_default"] forState:UIControlStateNormal];
        [self addSubview:_refreshBtn];
        _refreshBtn.center = CGPointMake(_storeTableView.center.x, _refreshBtn.center.y);
        WS(weakSelf);
        [_refreshBtn addButtonBlockControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
            [weakSelf getJWTStore];
        }];
        
        //移除视图
        _removeCurViewBtn = [[UIButton alloc] initWithFrame:CGRectMake(_courTableView.left+Cell_Margin, _columNameView.bottom+UISCREEN_HEIGHT*0.5+3*Cell_Margin, 22, 22)];
        [_removeCurViewBtn setBackgroundImage:[UIImage imageNamed:@"my_user_default"] forState:UIControlStateNormal];
        [self addSubview:_removeCurViewBtn];
        _removeCurViewBtn.center = CGPointMake(_courTableView.center.x, _refreshBtn.center.y);
        [_removeCurViewBtn addButtonBlockControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
            [weakSelf removeSuperView];
        }];
        
        [self addSubview:_swipeView];
        
        
        APPDELEGATE.courseType = [NSString stringWithFormat:@"%ld",_course_type+1];
        if (self.getCourseInfo) {
            self.getCourseInfo(APPDELEGATE.courseType);
        }
        
        [self getJWTStore];
    }
    return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    if ([touch.view isKindOfClass:[UIButton class]]){
        return NO;
    }
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//判断如果点击的是tableView的cell，就把手势给关闭了
        return NO;//关闭手势
    }
    return YES;
}

-(void) swipeToSelectCourse:(UISwipeGestureRecognizer *)rec{
    if (rec.direction == UISwipeGestureRecognizerDirectionRight) {
        [UIView animateWithDuration:1.0 animations:^{
            self.frame = CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT);
            _swipeView.backgroundColor = RGBA(0, 0, 0, 0.4);
        }];
        
    }else if(rec.direction == UISwipeGestureRecognizerDirectionLeft){
        [self removeSuperView];
    }
}

/**移除当前视图*/
-(void) removeSuperView{
    [UIView animateWithDuration:1.0 animations:^{
        self.frame = CGRectMake(-(UISCREEN_WIDTH*0.95), 0, UISCREEN_WIDTH, UISCREEN_HEIGHT);
        _swipeView.backgroundColor = [UIColor clearColor];
    }];
}

#pragma mark - 表格视图数据源代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _storeTableView) {
        return _storeArray.count;
    }else
        return _courseArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _storeTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:selectStore_identifier forIndexPath:indexPath];
        JWTStore *store = [JWTStore mj_objectWithKeyValues:_storeArray[indexPath.row]];
        cell.textLabel.text = store.nickname;
        cell.textLabel.font = Text_Font14;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.tag = indexPath.row;
        if (indexPath.row == _store_index) {
            cell.textLabel.textColor = HeaderBackground_Color;
        }else{
            cell.textLabel.textColor = RGB_DarkGray;
        }
        
        return  cell;
    }else if (tableView == _courTableView){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:selectCourse_identifier forIndexPath:indexPath];
        cell.textLabel.text = [_courseArray objectAtIndex:indexPath.row];
        cell.textLabel.font = Text_Font14;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.backgroundColor = RGB_LightGrayBackGround;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.tag = indexPath.row;
        if (indexPath.row == _course_type) {
            cell.textLabel.textColor = RGB_GREEN;
        }else{
            cell.textLabel.textColor = RGB_DarkGray;
        }
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _storeTableView) {
        _store_index = indexPath.row;
        [_storeTableView reloadData];
        JWTStore *store = [JWTStore mj_objectWithKeyValues:_storeArray[indexPath.row]];
        APPDELEGATE.storeID = store.storeid;
        APPDELEGATE.storeName = store.nickname;
        if (self.getAreaInfo) {
            self.getAreaInfo(APPDELEGATE.storeID,APPDELEGATE.storeName);
        }
        
    }else if(tableView == _courTableView){
        _course_type = indexPath.row;
        [_courTableView reloadData];
        APPDELEGATE.courseType = [NSString stringWithFormat:@"%ld",_course_type+1];
        if (self.getCourseInfo) {
            self.getCourseInfo(APPDELEGATE.courseType);
        }
        [self removeSuperView];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    static UITableViewCell *cell = nil;
    //    static dispatch_once_t onceToken;
    //    dispatch_once(&onceToken, ^{
    //        cell = [tableView dequeueReusableCellWithIdentifier:storeCell_identifier];
    //    });
    //    return cell.height;
    
    return 40;
}
/**
 获取门店信息
 */
-(void) getJWTStore{
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTStoreComboBox];
    [HUD showUIBlockingIndicator];
    [AFNHttpsRequest requestWithUrl:urlString WithParam:nil WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                NSLog(@"%@",model);
                _storeArray = [model objectForKey:@"data"];
                _store_index = 0;
                [_storeTableView reloadData];
                JWTStore *store = [JWTStore mj_objectWithKeyValues:_storeArray[0]];
                APPDELEGATE.storeID = store.storeid;
                APPDELEGATE.storeName = store.nickname;
                if (self.getAreaInfo) {
                    self.getAreaInfo(APPDELEGATE.storeID,APPDELEGATE.storeName);
                }
                
//                _tableView.frame = CGRectMake(self.left, 0, self.width, _storeArray.count*44);
                [_storeTableView reloadData];
            }else if ([[model objectForKey:@"200"]integerValue] == 500){
//                _tableView.frame = CGRectMake(self.left, 0, self.width, _storeArray.count*44);
                [HUD showToast:@"获取门店信息失败"];
            }
        }
    }];
}


@end
